package com.jiulan.hearthearing.ui.course.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsModel;
import com.jiulan.hearthearing.utils.TimeFormater;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class CourseFMMuluAdapter extends BaseQuickAdapter<CourseDetailsModel.DataBean.ClassListBean, BaseViewHolder> {
    private int clickId = -1;

    //进入单选页面执行这个方法，选中传过来的ID对应的item
    public void setClickPosition(int clickId) {
        this.clickId = clickId;
    }

    public CourseFMMuluAdapter(List<CourseDetailsModel.DataBean.ClassListBean> data) {
        super(R.layout.item_course_fm_mulu, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CourseDetailsModel.DataBean.ClassListBean item) {
        if (clickId == item.getId()) {
            helper.setTextColor(R.id.tvTitle, mContext.getResources().getColor(R.color.title_bg));
            helper.setGone(R.id.llAction, true);
            helper.setGone(R.id.ivPlay, false);
        } else {
            helper.setTextColor(R.id.tvTitle, mContext.getResources().getColor(R.color.black_light));
            helper.setGone(R.id.llAction, false);
            helper.setGone(R.id.ivPlay, true);
        }
        switch (item.getIs_free()) {
            case 0://收费
                helper.setGone(R.id.ivPay, true);
                break;
            case 1://免费
                helper.setGone(R.id.ivPay, false);
                break;
        }
        helper.setText(R.id.tvTitle, item.getName());
        int duration = 0;
        if (item.getFile_info() != null && !"".equals(item.getFile_info())) {
            try {
                JSONObject mJsonObject = new JSONObject(item.getFile_info());
                duration = mJsonObject.getInt("duration");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            duration = 0;
        }
        helper.setText(R.id.tvTotalTime, "总时长：" + TimeFormater.formatS(duration));
        helper.setText(R.id.tvHearingNum, String.valueOf(item.getPlay_num()) + "人已听");
        helper.addOnClickListener(R.id.ivPlay);
    }
}
